/*
 * 测试ArrayFile的API和基本操作
 * ArrayFile 是 MapFile的变种，将索引作为Key 从0开始，且索引是LongWritable
 */
package com.tjh.hadoop.file;

import static org.junit.Assert.assertEquals;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.io.ArrayFile;
import org.apache.hadoop.io.ArrayFile.Writer;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.MapFile;
import org.apache.hadoop.io.Text;
import org.junit.Test;

public class TestArrayFile {
	/**
	 * 测试写入ArrayFile文件
	 */
	@Test
	public void write() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.newInstance(conf); // conf中是本地文件系统 file:///
		// 创建写入器
		// 其中path指定的是文件夹 file:///f:/test/arrayfile.af,
		// ArrayFile 只需要指定 value类型, key 默认是索引	
		Writer writer = new ArrayFile.Writer(conf, fs, "file:///f:/test/arrayfile.af", Text.class);
		writer.setIndexInterval(16);
		
		for(int i=1; i<=200; i++) {
			if(i%5==0) {
				writer.append(new Text("tom" + i));
			}
		}
		writer.close();
	}
	/**
	 * 测试读ArrayFile文件
	 */
	@Test
	public void read() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.newInstance(conf);
		// 创建读入器
		MapFile.Reader reader = new ArrayFile.Reader(fs, "file:///f:/test/arrayfile.af", conf);
		
		LongWritable key = new LongWritable();
		Text value = new Text();
		
		while(reader.next(key, value)) {
			System.out.println(key + " : " + value);
		}
		
		reader.get(key, value);
		assertEquals(value.toString(), "tom200");
		
		reader.close();
	}
	/**
	 * 测试读ArrayFile文件
	 * 使用next()单个参数
	 */
	@Test
	public void read2() throws Exception {
		Configuration conf = new Configuration();
		FileSystem fs = FileSystem.newInstance(conf);
		// 创建读入器
		ArrayFile.Reader reader = new ArrayFile.Reader(fs, "file:///f:/test/arrayfile.af", conf);
		
		Text value = new Text();
		
		// ArrayFile可以 仅取值类型
		while(reader.next(value) != null) {
			System.out.println(value);
		}
		
		reader.close();
	}
}
